import uitest
import uicls
import uiconst
import trinity

class NewSprite(uitest.BaseDecoCore, uicls._GetDecoMetaclassInst('trinity.Tr2Sprite2d')):
    __guid__ = 'uicls.NewSprite'
    default_name = 'sprite'
    default_noScale = 0
    default_texture = None
    default_texturePath = None
    default_left = 0
    default_top = 0
    default_width = 64
    default_height = 64
    default_color = (1, 1, 1, 1)
    default_effect = trinity.S2D_RFX_COPY

    def ApplyAttributes(self, attributes):
        self.texture = Texture(_parentObject=self)
        self.texturePrimary = trinity.Tr2Sprite2dTexture()
        uitest.BaseDecoCore.ApplyAttributes(self, attributes)
        effect = attributes.get('effect', self.default_effect)
        if (effect is not None):
            self.SetEffect(effect)
        resPath = attributes.get('texturePath', self.default_texturePath)
        if resPath:
            self.texturePrimary.resPath = str(resPath)
        color = attributes.get('color', self.default_color)
        if (color is not None):
            self.SetRGB(*color)



    def LoadTexture(self, *args):
        return 



    def Invalidate(self):
        return 



    def SetEffect(self, effect):
        self.effect = effect



    def SetRGB(self, *color):
        self.color = color



    def GetRGB(self):
        return self.color[:3]



    def GetRGBA(self):
        return self.color



    def SetAlpha(self, alpha):
        current = list(self.color)
        current[3] = alpha
        self.color = current



    def GetAlpha(self):
        return self.color[3]



    def OnSetAttr(self, k, v):
        uitest.BaseDecoCore.OnSetAttr(self, k, v)
        if (k == 'rectLeft'):
            self.texturePrimary.srcX = v
        elif (k == 'rectTop'):
            self.texturePrimary.srcY = v
        elif (k == 'rectWidth'):
            self.texturePrimary.srcWidth = v
        elif (k == 'rectHeight'):
            self.texturePrimary.srcHeight = v



    def UpdateAlignment(self, budget = None):
        uitest.BaseDecoCore.UpdateAlignment(self, budget)




class Texture(dict):

    def __init__(self, **kw):
        dict.__init__(self, **kw)



    def __setattr__(self, attr, val):
        if (attr == 'pixels'):
            self['_parentObject'].texturePrimary.resPath = val
        self[attr] = val




